package com.java110.community.cmd.room;

import com.alibaba.fastjson.JSONObject;
import com.java110.bean.dto.room.RoomDto;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.cmd.Cmd;
import com.java110.core.cmd.CmdEvent;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.exception.CmdException;
import com.java110.core.utils.Assert;
import com.java110.core.utils.BeanConvertUtil;
import com.java110.core.utils.ListUtil;
import com.java110.intf.community.IRoomsTreeInnerServiceSMO;
import com.java110.vo.ApiRoomDataVo;
import com.java110.vo.ApiRoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "room.queryRoomsTree")
public class QueryRoomsTreeCmd extends Cmd {
    @Autowired
    private IRoomsTreeInnerServiceSMO roomsTreeInnerServiceSMOImpl;


    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
        Assert.jsonObjectHaveKey(reqJson, "communityId", "请求中未包含communityId信息");
        Assert.jsonObjectHaveKey(reqJson, "unitId", "请求中未包含单元信息");
        Assert.jsonObjectHaveKey(reqJson, "page", "请求报文中未包含page节点");
        Assert.jsonObjectHaveKey(reqJson, "row", "请求报文中未包含row节点");

        Assert.isInteger(reqJson.getString("page"), "page不是数字");
        Assert.isInteger(reqJson.getString("row"), "row不是数字");
        Assert.hasLength(reqJson.getString("communityId"), "小区ID不能为空");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        RoomDto roomDto = BeanConvertUtil.covertBean(reqJson, RoomDto.class);
        ApiRoomVo apiRoomVo = new ApiRoomVo();

        List<RoomDto> roomDtos = null;

        roomDtos = roomsTreeInnerServiceSMOImpl.queryRoomsTree(roomDto);

        apiRoomVo.setTotal(0);
        if (!ListUtil.isNull(roomDtos)) {
            roomDtos = deduplication(roomDtos);
            apiRoomVo.setTotal(roomDtos.size());
        }

        apiRoomVo.setRooms(BeanConvertUtil.covertBeanList(roomDtos, ApiRoomDataVo.class));
        int row = reqJson.getInteger("row");
        apiRoomVo.setRecords((int) Math.ceil((double) apiRoomVo.getTotal() / (double) row));

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(apiRoomVo), HttpStatus.OK);
        cmdDataFlowContext.setResponseEntity(responseEntity);
    }

    private List<RoomDto> deduplication(List<RoomDto> roomDtos) {
        List<RoomDto> newRoomDtos = new ArrayList<>();
        for(RoomDto roomDto : roomDtos){
            if(!hasInNewRoomDtos(newRoomDtos,roomDto)){
                newRoomDtos.add(roomDto);
            }
        }

        return newRoomDtos;
    }

    private boolean hasInNewRoomDtos(List<RoomDto> newRoomDtos, RoomDto roomDto) {
        if(ListUtil.isNull(newRoomDtos)){
            return false;
        }

        for(RoomDto newRoomDto : newRoomDtos){
            if(newRoomDto.getRoomId().equals(roomDto.getRoomId())){
                return true;
            }
        }

        return false;
    }


}
